package cn.huobi.org.controller;

import cn.huobi.basic.util.AjaxResult;
import cn.huobi.basic.util.PageList;
import cn.huobi.org.domain.Detail;
import cn.huobi.org.domain.Employee;
import cn.huobi.org.query.DetailQuery;
import cn.huobi.org.service.DetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DetailController {

    @Autowired
    private DetailService detailService;

    /**
     * 查询所有
     */
    @GetMapping
    public List<Detail> findAll(){
        return detailService.findAll();
    }

    /**
     * 查询单条
     */
    @GetMapping("/{id}")
    public Detail findById(@PathVariable("id") Long id){
        return detailService.findById(id);
    }

    /**
     * 添加或修改
     */
    @PutMapping
    public AjaxResult add(@RequestBody Detail detail){
        if (detail.getId() == null){
            // 添加
            detailService.add(detail);
        }else {
            // 修改
            detailService.update(detail);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult detele(@PathVariable("id") Long id){
        detailService.delete(id );
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Detail> queryPage(@RequestBody DetailQuery query){
        return detailService.queryByPage(query);
    }

    /**
     *  批量删除
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        detailService.batchDel(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/typesId/{typesId}")
    public List<Detail> findByTypesId(@PathVariable("typesId") Long typesId){
        return detailService.findByTypesId(typesId);
    }
}
